package von.seiji.cn.test;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import von.seiji.cn.asset.Base64DecodeMultipartFile;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

/**
 * @Author:Seiji
 * @Time:2020/12/7 21:06
 */
public class T5 {

    public static MultipartFile base64Convert(String base64) {
        String[] baseStrs = base64.split(","); //base64编码后的图片有头信息所以要分离出来   [0]data:image/png;base64, 图片内容为索引[1]

        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = new byte[0];
        try {
            b = decoder.decodeBuffer(baseStrs[1]); //取索引为1的元素进行处理
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }
        return new Base64DecodeMultipartFile(b, baseStrs[0]);//处理过后的数据通过Base64DecodeMultipartFile转换为MultipartFile对象

    }


    public String toBase64(InputStream inputStream){
        //Files.
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            IOUtils.copy(inputStream,stream);

            byte[] bytes1 = stream.toByteArray();
            System.out.println("验证："+(bytes1.length==inputStream.available()));
            return  prefix+Base64.getEncoder().encodeToString(bytes1);
        } catch (IOException e) {
            e.printStackTrace();
        }
return null;
    }

    final String prefix="data:image/jpg;base64,";

    public String toBase641(InputStream inputStream){
        //Files.
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            IOUtils.copy(inputStream,stream);
            byte[] bytes1 = stream.toByteArray();
            System.out.println("验证："+(bytes1.length==inputStream.available()));
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return   prefix+base64Encoder.encode(bytes1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String toBase642(InputStream inputStream){
        //Files.
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            IOUtils.copy(inputStream,stream);
          //  org.apache.commons.io.IOUtils.toInputStream()
//            StreamUtils.copyToString();
          //  StreamUtils.emptyInput();
         //FileCopyUtils.copyToByteArray()
           // Files.copy()
           // StreamUtils.copyToByteArray(inputStream)
            // org.apache.commons.io.IOUtils.toByteArray(is);
            byte[] bytes1 = stream.toByteArray();
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return   prefix+base64Encoder.encode(bytes1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Test
    public void test01(){
        String path="C:\\Users\\Administrator\\Desktop\\临时储物库\\技能识别照片\\863fe31c4c09940f0cada92c1d6b06d.jpg";
       // String destPath="C:\\Users\\Administrator\\Desktop\\临时储物库\\000000.jpg";
        Path destPath = Paths.get("C:\\Users\\Administrator\\Desktop\\临时储物库", "111111.jpg");
        try {
            FileInputStream stream = new FileInputStream(path);
            String s = toBase64(stream);
         //   System.out.println("s = " + s);
            MultipartFile multipartFile = base64Convert(s);
         //  multipartFile.transferTo(destPath);
            System.out.println("----------->完成");
            System.out.println("multipartFile.getName() = " + multipartFile.getName());
            System.out.println("------------->文件名："+multipartFile.getOriginalFilename());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void test02(){
        Path path = Paths.get("C:\\Users\\Administrator\\Desktop\\临时储物库", "000000.jpg");
        System.out.println(path);
      //  System.out.println(File);
    }

/*    @Test
    public void test03() throws JSONException {
        JSONArray jsonArray = new JSONArray("[{\"url\":\"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png\"}]");
        System.out.println(jsonArray.getJSONObject(0));
        String url = jsonArray.getJSONObject(0).getString("url");
        System.out.println("url = " + url);
        System.out.println(jsonArray);
        System.out.println(jsonArray.toString());
        System.out.println(String.valueOf(jsonArray));
    }*/


}
